<?php
// +----------------------------------------------------------------------
// | 留言模块
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: GuestbookAction.class.php 119 2011-05-13 08:05:33Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class GuestbookAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	public function listMessage() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'comment_id';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}		
		//查询过滤
		$map  =	 array();	
	
		/*1为日志评论 2为留言 3为其他评论(投票,关于等等*/
		$map['comment_type'] = 2;
		
		switch($_REQUEST['map']) {
			case 'visible':  $map['comment_visible'] = 0; break;		
			case 'approved': $map['comment_approved'] = 0; break;
        }		
			
		if ($_REQUEST['keyword']!='') {
			$map['comment_content'] = array('like',"%{$_REQUEST['keyword']}%");
		}
		if (!empty($_REQUEST['key'])) {
			$map = json_decode(base64_url_decode($_REQUEST['key']));
		}					
		$gb  =  M('Comment');
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );	
		$count = $gb->where($map)->count();
		$p = new Page ($count, C('admin_list_num'));
		$list = $gb->where($map)->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		//分页跳转的时候保证查询条件
		/*foreach ($map as $key => $val) {
			if (!is_array ($val)) {
				$p->parameter .= "$key=" . urlencode ( $val ) . "&";
			}
		}*/
		if (!empty($map)) {
			$p->parameter .= "&key=" . base64_url_encode(json_encode($map)) . "&";
		}
		//dump($list);
		$page = $p->show ();
		
		$sort = $sort.'_'.$order;

		$this->assign('ur_here',L('listMessage'));
		$this->assign('pagestyle',C('admin_page_style'));
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}	
	
	public function detailsMessage() {
	    $gb = M('Comment');
		$user =  D('AdminUser');
		$nick_name   =  $user->getUserNickName(true);
		$map['comment_id'] = $_REQUEST['id'];
		$vo	= $gb->where($map)->find();
		$vo['comment_content'] = convert_smilies($vo['comment_content']);
		if ($vo['comment_recontent']) {
			$vo['comment_recontent'] = convert_smilies($vo['comment_recontent']);
		}
		
		
		/* 上一条留言下一条留言 */
        $prev_msg_id = $gb->where('comment_type =2 and comment_id > '.$_REQUEST['id'])->limit(1)->getField('comment_id');

        if (!empty($prev_msg_id)) {
			$this->assign('prev_msg_id',$prev_msg_id);//上一条留言
        }
		
		$next_msg_id = $gb->where('comment_type =2 and comment_id < '.$_REQUEST['id'])->getField('max(comment_id)');
		
        if (!empty($next_msg_id)) {
			$this->assign('next_msg_id',$next_msg_id);//下一条留言
        }
		
		$this->assign('action_link', array('href'=>__URL__.'/listMessage/', 'text' => L('listMessage')));
		$this->assign('nick_name', $nick_name);
		$this->assign('vo',$vo);
		$this->assign('ur_here',L('detailsMessage'));
		$this->display();
	}
	
	//快速切换
	public function quickToggle() {
		$gb = M('Comment');
		if ($_REQUEST['act'] == 'visible') {
			$map['comment_id']	 =	$_REQUEST['id'];
			$v = $_REQUEST['v'];
			$list = $gb->where($map)->setField('comment_visible', $v);
			if ($list!==false) {
				clear_cache_files();    // 清除缓存
				set_log('edit(Guestbook, Toggle, Visible, Id: '.$_REQUEST['id'].')');
				redirect(__URL__.'/detailsMessage/id/'.$_REQUEST['id']);
			} else {
				$this->error (L('error'));
			}
		} elseif ($_REQUEST['act'] == 'approved') {
			$map['comment_id']	 =	$_REQUEST['id'];
			$v = $_REQUEST['v'];
			$list = $gb->where($map)->setField('comment_approved', $v);
			if ($list!==false) {
				set_log('edit(Guestbook, Toggle, Approved, Id: '.$_REQUEST['id'].')');
				redirect(__URL__.'/detailsMessage/id/'.$_REQUEST['id']);
			} else {
				$this->error (L('error'));
			}
		} else {
			$this->error (L('illegal'));
		}	
	}
	
	//批量切换
	public function batchMessage() {
		$gb = M('Comment');
		$select =  isset($_POST['select']) ? $_POST['select'] : '';
		if ($select) {
			if ($_POST['act'] == 'visible') {
				$pk   =  $gb->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $gb->where($condition)->setField('comment_visible', 1);
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('edit(Guestbook, Toggle, Visible, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/listMessage/');
					$this->success (sprintf(L('batch_visible'), count($_POST['key'])));
				} else {
					$this->error (L('error'));
				}
			}
			if ($_POST['act'] == 'approved') {
				$pk   =  $gb->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $gb->where($condition)->setField('comment_approved', 1);
				if ($list!==false) {
					//clear_cache_files();    // 清除缓存
					set_log('edit(Guestbook, Toggle, Approved, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/listMessage/');
					$this->success (sprintf(L('batch_approved'), count($_POST['key'])));
				} else {
					$this->error (L('error'));
				}
			}
			if ($_POST['act'] == 'drop') {
				$pk   =  $gb->getPk();
				$condition = array ($pk => array ('in', $_POST['key']));
				$list = $gb->where($condition)->delete();
				if ($list!==false) {
					clear_cache_files();    // 清除缓存
					set_log('drop(Guestbook, Count: '.count($_POST['key']).')');
					$this->assign('jumpUrl',__URL__.'/listMessage/');
					$this->success (sprintf(L('batch_drop'), count($_POST['key'])));
				} else {
					$this->error (L('error'));
				}
			}
		} else {
			$this->error (L('illegal'));
		}
	}
	
	//管理员回复
	public function replayMessage() {
		$gb = D('Comment');
		$data = $gb->create();
		if (false === $data) {
			$this->error($com->getError());
		} else {
			$gb->comment_remail = get_gravatar($_SESSION['user_email'], $s = 80, $d = C('avatar_default'), $r = C('avatar_rating'));
			$result = $gb->save();
			if (false !== $result) {
				set_log('edit(Guestbook, Replay, Id: '.$data['comment_id'].')');
				redirect(__URL__.'/detailsMessage/id/'.$data['comment_id']);
			} else {
				$this->error(L('error'));
			}
		}
	}
	
	//删除
	public function dropMessage() {
		$gb = M('Comment');
		$map['comment_id']	 =	$_REQUEST['id'];
		$list = $gb->where($map)->delete();
		if ($list !== false) {
			clear_cache_files();    // 清除缓存
			set_log('drop(Guestbook, Id: '.$_REQUEST['id'].')');
			$this->assign('jumpUrl',__URL__.'/listMessage/');
			$this->success(L('success'));
		} else {
			$this->error (L('error'));
		}
	}

}

?>